package com.cloudsigns.factorymanager2.engineering.adapter;

import android.content.Context;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ImageView;

import com.cloudsigns.factorymanager2.R;
import com.cloudsigns.factorymanager2.engineering.bean.EngineeringConstructContentBean;

import com.cloudsigns.factorymanager2.project.myinerface.ItemRemoveListener;

import java.util.ArrayList;
import java.util.List;

/**
 * 编辑施工内容
 * Created by tony on 2018/1/29.
 */

public class EditConstructContentAdapter extends BaseAdapter {
    private Context context;
    private List<EngineeringConstructContentBean> datas = new ArrayList<EngineeringConstructContentBean>();
    private LayoutInflater inflater;
    private String tag = "";
    private ItemRemoveListener itemRemoveListener;
    private TextChangeLiastener textChangeLiastener;
    private int mTouchItemPosition = -1;


    public EditConstructContentAdapter(Context context, String tag) {
        this.context = context;
        this.inflater = LayoutInflater.from(context);
        this.tag = tag;
    }

    public void setItemRemoveListener(ItemRemoveListener itemRemoveListener) {
        this.itemRemoveListener = itemRemoveListener;
    }

    public void setTextChangeLiastener(TextChangeLiastener textChangeLiastener) {
        this.textChangeLiastener = textChangeLiastener;
    }


    public void setData(List<EngineeringConstructContentBean> list) {
        this.datas.clear();
        if (null != list) {
            this.datas.addAll(list);
        }

    }


    @Override
    public int getCount() {
        return datas.size();
    }

    @Override
    public EngineeringConstructContentBean getItem(int position) {
        return datas.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup viewGroup) {
        final ViewHolder holder;
        if (convertView == null) {
            holder = new ViewHolder();

            convertView = inflater.inflate(R.layout.item_edit_construct_content_lv, null);
            holder.img_remove = convertView.findViewById(R.id.img_remove);
            holder.edt_content = convertView.findViewById(R.id.edt_content);
            holder.img_add=convertView.findViewById(R.id.img_add);

            holder.edt_content.setOnTouchListener(new View.OnTouchListener() {
                @Override
                public boolean onTouch(View view, MotionEvent motionEvent) {
                    mTouchItemPosition = (int) view.getTag();
                    return false;
                }
            });

            holder.myWatcher = new MyWatcher();
            holder.edt_content.addTextChangedListener(holder.myWatcher);
            holder.updatePosition(position);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
            holder.updatePosition(position);
        }

        holder.img_remove.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (itemRemoveListener != null) {
                    itemRemoveListener.remove((int) holder.edt_content.getTag());
                }
            }
        });
        EngineeringConstructContentBean bean = datas.get(position);
//        holder.edt_content.setText(bean.getContent());

        holder.edt_content.setTag(position);

        if (mTouchItemPosition == position) {
            holder.edt_content.requestFocus();
            holder.edt_content.setSelection(holder.edt_content.getText().length());
        } else {
            holder.edt_content.clearFocus();
        }
        return convertView;
    }

    private class ViewHolder {
        private ImageView img_remove;
        private EditText edt_content;
        private MyWatcher myWatcher;
        private ImageView img_add;

        public void updatePosition(int position) {
            myWatcher.updatePosition(position);
        }
    }

    class MyWatcher implements TextWatcher {
        private int mPosition;


        public void updatePosition(int position) {
            mPosition = position;
        }


        @Override
        public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

        }

        @Override
        public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

        }

        @Override
        public void afterTextChanged(Editable editable) {
//            datas.get(mPosition).setContent(editable.toString());
            if (textChangeLiastener != null) {
                textChangeLiastener.textChange(mPosition, editable.toString());
            }
        }
    }

    public interface TextChangeLiastener {
        public void textChange(int posittion, String content);
    }
}
